class Dep{
    constructor(){
        //this.id = i++
        this.subs = []
    }

    //添加一个watcher
    addSub(sub){
        this.subs.push(sub)
    }
    //移除
    removeSub(sub){
        for(let i = this.subs.length - 1; i >= 0 ; i--){
            if(sub === this.subs[i]){
                this.subs.splice(i,1)
            }
        }
    }

    //将当前Dep与当前的watcher（暂时渲染watcher）关联
    depend(){
        if(Dep.target){
            this.addSub(Dep.target)  //将当前watcher与当前dep关联
            Dep.target.addDep(this)  //将当前watcher与当前dep关联
        }
    }

    //触发与之关联的watcher的update方法，起到更新的作用
    notify(){
        //在真实的vue中是依次触发this.subs中的watcher的update方法
        if(Dep.target){
            Dep.target.update()
        }
    }
}

//全局的容器存储渲染watcher
//let globalWatcher
//学vue实现
Dep.target = null  //这是全局的watcher

let targetStack = []

function pushTarget(target){
    targetStack.unshift(Dep.target)
    Dep.target = target
}

function popTarget(){
    targetStack.shift()
}